package com.moyun.controller;


import com.moyun.common.annotation.CheckPermission;
import com.moyun.common.utils.AnnotationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author weiwenbin
 * @Date 2020/5/20 上午9:40
 */
@ApiIgnore
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private AnnotationUtil annotationUtil;
    @Autowired
    private Environment environment;

    @PostMapping("/justTest")
    @CheckPermission(descrption = "测试-只是个测试")
    public void justTest() {
        System.out.println("我只是个测试方法");
    }

    @PostMapping("/justTest2")
    @CheckPermission(descrption = "测试-只是个测试2")
    public void justTest2() throws Exception {
        String a = this.environment.getProperty("a");

        annotationUtil.getAllAddTagAnnotationUrl("classpath:/com/copyrightagency/foresystem/analysis/**/*.class", CheckPermission.class);
    }


    private final String BASE_PACKAGE = "com.copyrightagency.foresystem.analysis";
    private final String RESOURCE_PATTERN = "/**/*.class";

    @PostMapping("/justTest3")
    public void justTest3() {
        Map<String, Class<?>> handlerMap = new HashMap();

        //spring工具类，可以获取指定路径下的全部类
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        try {
            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
                    ClassUtils.convertClassNameToResourcePath(BASE_PACKAGE) + RESOURCE_PATTERN;
            Resource[] resources = resourcePatternResolver.getResources(pattern);
            //MetadataReader 的工厂类
            MetadataReaderFactory readerfactory = new CachingMetadataReaderFactory(resourcePatternResolver);
            for (Resource resource : resources) {
                //用于读取类信息
                MetadataReader reader = readerfactory.getMetadataReader(resource);
                //扫描到的class
                String classname = reader.getClassMetadata().getClassName();
                Class<?> clazz = Class.forName(classname);
                //判断是否有指定主解
//                MyAnno anno = clazz.getAnnotation(MyAnno.class);
//                if (anno != null) {
//                    //将注解中的类型值作为key，对应的类作为 value
//                    handlerMap.put(classname, clazz);
//                }
                Service anno = clazz.getAnnotation(Service.class);
                if (anno != null) {
                    //将注解中的类型值作为key，对应的类作为 value
                    handlerMap.put(classname, clazz);
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("哎呦！出错了呦！");
        }

        System.out.println("走完了呦！");
    }

}
